package com.shanzmoo.admin.web.store;

import com.shanzmoo.admin.domain.dto.StoreStockInitBillDetailUpdateDTO;
import com.shanzmoo.admin.domain.dto.StoreStockInitBillUpdateDTO;
import com.shanzmoo.admin.manager.store.StoreStockInitBillManager;
import com.shanzmoo.base.bean.PageRespBase;
import com.shanzmoo.base.bean.SHttpResponse;
import com.shanzmoo.base.bean.SResponse;
import com.shanzmoo.base.constant.Constant;
import com.shanzmoo.core.cache.CurrentUserCache;
import com.shanzmoo.db.store.entity.StoreStockInitBillDetailEntity;
import com.shanzmoo.db.store.entity.query.StoreStockInitBillQuery;
import com.shanzmoo.db.store.entity.vo.StoreStockInitBillVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.List;

/**
 * 库存期初导入单 控制层
 *
 * @author fs 2021-03-04
 */
@Api(tags = "仓储-库存期初导入单")
@RestController
@RequestMapping("/store/stock-init-bill")
@Slf4j
public class StoreStockInitBillController {

    @Autowired
    private StoreStockInitBillManager storeStockInitBillManager;

    @ApiOperation(value = "分页列表")
    @PostMapping("/pageList")
    public SHttpResponse<PageRespBase<StoreStockInitBillVO>> pageList(@Valid @RequestBody StoreStockInitBillQuery query) {
        PageRespBase<StoreStockInitBillVO> pageList = storeStockInitBillManager.queryPage(query);
        return SHttpResponse.ok(pageList);
    }

    @ApiOperation(value = "作废")
    @GetMapping("/cancel")
    public SHttpResponse<Boolean> cancel(@RequestParam(value = "id") Integer id) {
        storeStockInitBillManager.cancel(id, CurrentUserCache.getUserId());
        return SHttpResponse.ok(Boolean.TRUE);
    }

    @ApiOperation(value = "批量导入期初库存")
    @PostMapping("/import")
    public SHttpResponse<Boolean> importByExcel(@RequestParam("file") MultipartFile file) {
        if (file == null || file.isEmpty()) {
            log.error("文件上传失败，上传的文件为空！");
            return SHttpResponse.fail(Constant.ERRCODE_PARAM, "上传文件不能为空");
        }
        try{
            storeStockInitBillManager.importByExcel(file);
            return SHttpResponse.ok(Boolean.TRUE);
        }catch (Exception e){
            return SHttpResponse.ok(Boolean.FALSE);
        }
    }

    @ApiOperation(value = "查看明细")
    @GetMapping("/info")
    public SHttpResponse<StoreStockInitBillVO> info(@RequestParam("id") Integer id) {
        return SHttpResponse.ok(storeStockInitBillManager.info(id));
    }

    @ApiOperation(value = "审核入库")
    @GetMapping("/exmine")
    public SHttpResponse<Boolean> exmine(
            @NotEmpty(message = "单据id不允许为空") @ApiParam(value = "单据id") @RequestParam(value = "id") Integer id,
            @ApiParam(value = "备注") @RequestParam(value = "remark", required = false) String remark) {
        storeStockInitBillManager.exmine(id, remark);
        return SHttpResponse.ok(Boolean.TRUE);
    }

    @ApiOperation(value = "提交数据")
    @PostMapping("/push")
    public SHttpResponse<Boolean> push(@Valid @RequestBody StoreStockInitBillUpdateDTO updateDto) {
        List<StoreStockInitBillDetailUpdateDTO> tmps = updateDto.getDetails();
        List<StoreStockInitBillDetailEntity> details = new ArrayList<>();
        for(StoreStockInitBillDetailUpdateDTO detail:tmps){
            details.add(detail.convertEntity());
        }
        storeStockInitBillManager.push(updateDto.convertEntity(),details);
        return SHttpResponse.ok(Boolean.TRUE);
    }

 }
